<template>
	<view class="login">
		<image class="bg" src="/static/img/mine/bg.png"></image>
		<view class="container">
			<view class="go-back" @tap="goBack">
				<view></view>
			</view>
			<image class="logo" src="/static/img/common/logo.png"></image>
			<!-- <text class="password-login" @tap="$open('/pages/common/password-login/password-login')">账号密码登陆</text> -->
			<!-- <text class="phone-login" @tap="$open('/pages/common/mobile-login/mobile-login')">手机号登陆</text> -->
			<button class="login-btn" open-type="getUserInfo" @getuserinfo='getUserInfo'>微信用户快速登录</button>
			<image class="tip" src="/static/img/common/tip.png"></image>
		</view>
	</view>
</template>

<script>
	import {
		login
	} from '@/commen/js/config'
	import storage from '@/commen/utils/storage'

	export default {
		data() {
			return {
				invite_people_id: '',
				store_id: ''
			}
		},
		onShow() {
			if (storage.get('referrerId')) {
				this.invite_people_id = storage.get('referrerId')
			}
			if (storage.get('storeId')) {
				this.store_id = storage.get('storeId')
			}
		},
		methods: {
			// 获取用户信息
			async getUserInfo(e) {
				uni.showLoading({
					title: '登陆中'
				});
				let [codeErr, code] = await this.login()
				if (codeErr) {
					uni.hideLoading()
				}
				let [infoErr, userInfo] = await this.getInfo()
				if (infoErr) {
					uni.hideLoading()
				}
				const params = {
					code: code,
					invite_people_id: this.invite_people_id || '',
					nick_name: userInfo.nickName,
					headimgurl: userInfo.avatarUrl,
					store_id: this.store_id || ''
				}
				let [err, res] = await login(params)
				if (res && res.status == 1 && res.code == 200) {
					storage.set('token', res.token)
					this.$toast('登录成功')
					uni.$emit('refreshIndex')
					setTimeout(() => {
						this.$open('/pages/index/recommend/recommend', 3)
					}, 1500)
				}
				if (err) {
					uni.hideLoading()
				}
			},
			// 获取用户信息
			getInfo() {
				return new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							// 获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success(res) {
									resolve(res.userInfo)
								},
								fail(err) {
									reject(err)
								}
							});
						}
					});
				}).then(res => [null, res]).catch(err => [err])
			},
			// 登陆
			login() {
				return new Promise((resolve, reject) => {
					uni.getProvider({
						service: 'oauth',
						success(res) {
							if (!~res.provider.indexOf('weixin')) return
							uni.login({
								provider: 'weixin',
								success(res) {
									resolve(res.code)
								},
								fail(err) {
									reject(err)
								}
							})
						},
						fail(err) {
							reject(err)
						}
					})
				}).then(res => [null, res]).catch(err => [err])
			},
			// 返回
			goBack() {
				this.$close(1)
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/scss/mixin.scss';
	@import '@/static/scss/config.scss';

	.login {
		position: relative;
		@include box-w(100%);

		.bg {
			@include square();
			display: block;
		}

		.container {
			@include square();
			@include cross();
			@include flex-col(flex-start);
			padding-top: 50rpx;

			.go-back {
				position: absolute;
				top: 110rpx;
				left: 0;
				@include box(100rpx, 50rpx);
				@include flex-row();

				&>view {
					@include box-bg(16rpx, 28rpx, '');
				}
			}

			.logo {
				@include box(412rpx, 568rpx);
			}

			.password-login,
			.phone-login {
				@include font-set($font-a, $color-a, bold);
			}

			.password-login {
				@include bottom(630rpx);
			}

			.phone-login {
				@include bottom(560rpx);
			}

			.login-btn {
				@include bottom(342rpx);
				@include box(448rpx, 120rpx, $bg-a);
				@include flex-row();
				border-radius: 60rpx;
				@include font-set($font-a, $color-a);
			}

			.tip {
				@include bottom(22rpx);
				@include square(240rpx);
			}
		}
	}
</style>
